package com.atguigu.p_homework_adc_123_lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

// 判断 干活 通知
class Printer{
    String s = null;
    Lock lock = new ReentrantLock();
    Condition condition = lock.newCondition();

    public void printAbc(){
        lock.lock();
        try{
            while (s!=null&&s=="abc"){
                try {
                    condition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            s = "abc";
            System.out.println(Thread.currentThread().getName()+s);
            condition.signalAll();
        }finally {
            lock.unlock();
        }
    }

    public void print123() throws InterruptedException {
        lock.lock();
        try {
            while (s!=null&&s=="123"){
                condition.await();
            }
            s = "123";
            System.out.println(Thread.currentThread().getName()+s);
            condition.signalAll();
        }finally {
            lock.unlock();
        }
    }
}

public class Test {
    public static void main(String[] args) {
        Printer printer = new Printer();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                printer.printAbc();
            }
        }, "t1").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    printer.print123();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "t2").start();
    }
}
